package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.CartDAO;
import org.csu.mypetstore.persistence.ItemDAO;
import org.csu.mypetstore.persistence.impl.CartDAOImpl;
import org.csu.mypetstore.persistence.impl.ItemDAOImpl;

public class CartService {
    private CartDAO cartDAO;
    private ItemDAO itemDAO;


    public int getQuantityById(String pId,String uId)
    {
        cartDAO = new CartDAOImpl();
        try{
            return cartDAO.getQuantityById(pId,uId);
        }catch (Exception e)
        {
            return 0;
        }
    }

    public Cart getCartByUserId(String userId)
    {
        cartDAO = new CartDAOImpl();
        try{
            return cartDAO.getCartByUserId(userId);
        }catch (Exception e)
        {
            return null;
        }
    }


    public void removeCartByUserId(String userId){
        cartDAO = new CartDAOImpl();
        try{
            cartDAO.removeCartByUserId(userId);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void removeItemByUserIdAndItemId(String userId,String itemid){
        cartDAO = new CartDAOImpl();
        try{
            cartDAO.removeItemByUserIdAndItemId(userId,itemid);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public void addItem(String userId, Item item){
        cartDAO = new CartDAOImpl();
        try{
            cartDAO.addItem(userId,item);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void addItem(String userId,String itemId){
        cartDAO = new CartDAOImpl();
        itemDAO = new ItemDAOImpl();
        try{
            Item item = itemDAO.getItem(itemId);
            cartDAO.addItem(userId,item);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void incrementQuantityByItemId(String userId, Item item){
        cartDAO = new CartDAOImpl();
        try{
            cartDAO.incrementQuantityByItemId(userId,item);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void incrementQuantityByItemId(String userId, String itemId) {
        cartDAO = new CartDAOImpl();
        itemDAO = new ItemDAOImpl();
        try {
            Item item = itemDAO.getItem(itemId);
            cartDAO.incrementQuantityByItemId(userId, item);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void decrementQuantityByItemId(String userId, Item item)
    {
        cartDAO = new CartDAOImpl();
        try{
            cartDAO.decrementQuantityByItemId(userId,item);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void decrementQuantityByItemId(String userId, String itemId) {
        cartDAO = new CartDAOImpl();
        itemDAO = new ItemDAOImpl();
        try {
            Item item = itemDAO.getItem(itemId);
            cartDAO.decrementQuantityByItemId(userId, item);
        } catch (Exception ignored) {
        }
    }

    public void updateCartByUserId(String userId, Cart cart){
        cartDAO = new CartDAOImpl();
        try
        {
            cartDAO.updateCartByUserId(userId,cart);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}